package com.demoweb.tetris;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.demoweb.model.dao.BoardDao;
import com.demoweb.model.dao.MemberDao;
import com.demoweb.model.dao.MembershipDao;
import com.demoweb.model.dto.Board;
import com.demoweb.model.dto.BoardComment;
import com.demoweb.model.dto.Membership;

@WebServlet("/tetris/writeform0.action")
public class CommentWriteServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//1. 브라우저에 사용자가 입력한 데이터를 읽어서 변수에 저장 (요청 정보에서 데이터 읽기)
				
//				String RegDate = req.getParameter("Date");
				String title = req.getParameter("title");
				String content = req.getParameter("content");
				Membership member = 
					(Membership)req.getSession().getAttribute("loginuser");
//		//1. dto객체 생성 -> 입력 정보 읽어서 dto객체에 저장
//		int boardNo = Integer.parseInt(req.getParameter("boardno"));
//
//		BoardComment comment = new BoardComment();
//		comment.setContent(req.getParameter("content"));
//		comment.setWriter(
//			((Membership)req.getSession().getAttribute("loginuser")).getMemberId());

		//2. 데이터베이스에 데이터 insert
//		BoardDao dao = new BoardDao();
//		dao.insertComment(comment);

//		//3. boardview로 이동
//		String pageNo = req.getParameter("pageno");
//		resp.sendRedirect(
//			"view0.action?boardno=" + boardNo + "&pageno=" + pageNo);
				
		
//				if (req.getSession().getAttribute("loginuser") == null) {
//					resp.sendRedirect(
//						"/demoweb/account/loginform.action?" + 
//						"returnuri=" + req.getRequestURI());
//					return;
//				}
		Board board = new Board();
		board.setWriter(member.getMemberId());
		board.setTitle(title);
		board.setGameNo(3);
		board.setGroupNo(3);
		board.setStep(3);
//		board.setRegDate(regDate);
		board.setContent(content);
		
		//2. DAO 객체를 이용해서 데이터베이스에 데이터 저장
		BoardDao dao = new BoardDao();
		dao.insertBoard(board);
	
		
		//3. 목록 페이지로 이동
		resp.sendRedirect("/demoweb/tetris/list0.action");
		
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		doGet(req, resp);
	}
	
}




